/*
	UnitConverter.cpp
	Christopher Raffaele
	04.28.2008

	UnitConverter's class definitions
*/
#include"UnitConverter.h" //UnitConverter class declarations

using namespace std;

//print the main menu
void UnitConverter::showMainMenu(){
	std::cout << "\n--------------------------------\n"
		 << "\\         Unit Converter        \\\n"
		 << " ---------------------------------\n"
		 << " | 1 | Distance                  |\n"
		 << " ---------------------------------\n"
		 << " | 2 | Temperature               |\n"
		 << " ---------------------------------\n"
		 << " | 3 | Time                      |\n"
		 << " ---------------------------------\n";
}

//display the main menu and launches the unit converter specified by the user
void UnitConverter::run(){
	showMainMenu();

	launchConverter(getUserChoice());
}

//launch the converter specified by the user
void UnitConverter::launchConverter(unsigned int userChoice){
	if(userChoice == 1){
		DistanceConverter *distanceConverter = new DistanceConverter();
		distanceConverter->start();
		delete distanceConverter;
	}
	
	if(userChoice == 2){
		TemperatureConverter *temperatureConverter = new TemperatureConverter();
		temperatureConverter->start();
		delete temperatureConverter;
	}
	
	if(userChoice == 3){
		TimeConverter *timeConverter = new TimeConverter();
		timeConverter->start();
		delete timeConverter;
	}
}

unsigned int UnitConverter::getUserChoice(){
	cout << "\nWhat units would you like to convert? ";

	unsigned int userChoice = 0;

	if(cin.width() != 0) cin.ignore(1000, '\n');

	//input validation
	do{
		cin >> userChoice;

		//invalid input
		if(cin.fail()){
			cin.clear();
			cin.ignore(1000, '\n');
			userChoice = 0;
		}
	//input must be 1, 2, or 3
	}while(userChoice < 1 || userChoice > 3);

	return userChoice;
}

bool UnitConverter::runAgain(){
	cout << "\nPerform conversions on different units(y/n)? ";

	cin.ignore(1000, '\n');

	//check only the first char
	return (cin.get() == 'y');
}